﻿@{
    ViewBag.Title = "MaterialListEdit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@{
    var action = ViewData["action"].ToString();
    string Id = ViewData["Id"].ToString();
    string width = ViewData["width"].ToString();
    var materiallist = ViewBag.materiallist; 
}
<style>
    .layui-form-select dl {
        max-height: 200px;
    }
</style>
<div class="mod_dialog" style="display: block; margin-top: @width; left: 0px;width:800px">
    <form id="frm" class="layui-form" lay-filter="frm">
        <div class="hd">
            @{if (action == "add")
            {
                <h3>添加</h3>
            }
            else
            {<h3>修改</h3>}
            }
        </div>
        <div class="bd">
            <table>
                <tr style="width:100%">
                    <td style="width:50%">
                        <div class="form clearfix nouse" data-col="0">
                            <ul class="form clearfix">
                                <li>
                                    <label class="label">物料编码：</label>
                                    <div class="inptxt" style="width:200px;">
                                        <p></p><input id="imgurl" type="hidden" name="M_IMGURL" lay-filter="M_IMGURL" />
                                        <input id="id" type="hidden" name="MATERIAL_ID" lay-filter="MATERIAL_ID" />
                                        <input type="text" name="M_CODE" lay-filter="M_CODE" lay-verify="required" autocomplete="off" class="layui-input">
                                    </div>
                                </li>

                                <li>
                                    <label class="label">物料型号：</label>
                                    <div class="inptxt">
                                        <p></p>
                                        <input type="text" name="M_SPECIFICATION" lay-filter="M_SPECIFICATION" lay-verify="required" autocomplete="off" class="layui-input">
                                    </div>
                                </li>
                                <li>
                                    <label class="label">厂家：</label>
                                    <div class="inptxt">
                                        <p></p>
                                        <input type="text" name="M_COMPANY" lay-filter="M_COMPANY" lay-verify="required" autocomplete="off" class="layui-input">
                                    </div>
                                </li>
                                <li>
                                    <label class="label">类型：</label>
                                    <div class="inptxt">
                                        <p></p>
                                        <input type="text" name="M_TYPE" lay-filter="M_TYPE" lay-verify="required" autocomplete="off" class="layui-input">
                                    </div>
                                </li>
                                <li>
                                    <label class="label">最小包数量：</label>
                                    <div class="inptxt">
                                        <p></p>
                                        <input type="text" name="M_NUMBER" lay-filter="M_NUMBER" lay-verify="number" autocomplete="off" class="layui-input">
                                    </div>
                                </li>
                                <li>
                                    <label class="label">物料状态：</label>
                                    <div class="inptxt" style="width:200px">
                                        <p></p>
                                        <select name="M_STATUS" lay-filter="M_STATUS" class="layui-inline">
                                            <option value="0">启用</option>
                                            <option value="1">停用</option>
                                        </select>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </td>
                    <td style="vertical-align:top;">
                        <div class="layui-upload" style="margin-top:40px">
                            <button type="button" class="layui-btn layui-btn-xs" id="test1">上传图片</button>
                            <div class="layui-upload-list">
                                @{if (materiallist.M_IMGURL == "" || materiallist.M_IMGURL == null)
                                {
                                    <img class="layui-upload-img" id="demo1" style="height:220px" src="~/Content/images/occupying.png">
                                }
                                else
                                {
                                    <img class="layui-upload-img" id="demo1" style="height:220px" src="@materiallist.M_IMGURL">

                                }
                                }
                                <p id="demoText"></p>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
        <div class="fd">
            <div class="load_btn">
                @{if (action == "add")
                {
                    <button class="btn_green_m" id="btnAdd" lay-submit="" lay-filter="btnAdd">保存添加</button>
                    <button class="btn_gray_m" id="btnCancel">取消添加</button>
                }
                else
                {<button class="btn_green_m" id="btnEdit" lay-submit="" lay-filter="btnEdit">保存修改</button>
                    <button class="btn_gray_m" id="btnCancel" style="height:33px;width:145px">取消修改</button>
                }}
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        if ("@action" == "edit") {
            $.ajax({
                url: "/Setting/GetMaterialInfoById/@Id",
                type: "post",
                dataType: "json",
                success: function (data) {
                    var form = layui.form;
                    form.val("frm", data);
                    form.render(); //刷新select选择框渲染
                }
            });
        }
        var form = layui.form;
        form.render(); //刷新select选择框渲染
        form.render('select');
        form.on('submit(btnAdd)', function (data) {
            var imgurl = $('#imgurl').val()
            var info = $.extend(data.field, { MATERIAL_ID: "@Id", M_IMGURL: imgurl });
            com.ajax({
                url: "/Setting/AddMaterial", type: "post", dataType: "json", data: info, success: function (data) {
                    parent.$("#infoList").datagrid("load");
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }
            });
            return false;
        });

        form.on('submit(btnEdit)', function (data) {
            var imgurl = $('#imgurl').val();
            var info = $.extend(data.field, { MATERIAL_ID: "@Id", M_IMGURL: imgurl  });
            com.ajax({
                url: "/Setting/EditMaterial", type: "post", dataType: "json", data: info, success: function (data) {
                    parent.$("#infoList").datagrid("load");
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }
            });
            return false;
        });
    });

    $("#btnCancel").click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
        return false;
    });

</script>
<script>
    layui.use('upload', function () {
        var $ = layui.jquery
        , upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
          , url: '/Setting/upload/'
           , data: {
               url: function () {
                   return $('#imgurl').val();
               }
           }
          , method: 'post'
            , field: 'file'
            , auto: true
          , before: function (obj) {
              //预读本地文件示例，不支持ie8
              obj.preview(function (index, file, result) {
                  $('#demo1').attr('src', result); //图片链接（base64）
              });
          }
          , done: function (tempObj) {
              var url=tempObj["imgurl"];
              if(url=="")
              {
                  $('#imgurl').val("~/Content/images/occupying.png")
              }
              else
              {
                  $('#imgurl').val(url);
              }
              
              return layer.msg(tempObj["message"]);
          }
          , error: function () {
              //演示失败状态，并实现重传
              var demoText = $('#demoText');
              demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
              demoText.find('.demo-reload').on('click', function () {
                  uploadInst.upload();
              });
          }
        });
    });
</script>